﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Universe.OneC.DomainModel;

namespace Universe.OneC.ConsoleSample
{
    class TestDataProcessor
    {
        public static void Run()
        {
            {
                InfoBase ib = new InfoBase(new ServerInfo(1, "localhost"));
                ib.UserName = "Администратор";
                ib.FileBase = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"1C\DemoHRM");
                Run(ib, AdapterUsageMode.Application);
            }

            {
                InfoBase ib = new InfoBase(new ServerInfo(1, "localhost"));
                ib.UserName = "Администратор";
                ib.FileBase = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"1C\DemoHRM");
                Run(ib, AdapterUsageMode.Connector);
            }

            {
                InfoBase ib = new InfoBase(new ServerInfo(1, "localhost"));
                ib.UserName = "Администратор";
                ib.Name = "ЗиУП-2.5.7.2";
                Run(ib, AdapterUsageMode.Application);
            }

            {
                InfoBase ib = new InfoBase(new ServerInfo(1, "localhost"));
                ib.UserName = "Администратор";
                ib.Name = "ЗиУП-2.5.7.2";
                Run(ib, AdapterUsageMode.Connector);
            }
        }
        
        public static void Run(InfoBase ib, AdapterUsageMode mode)
        {
            using (Adapter1C a = new Adapter1C(AdapterUsageMode.Application))
            {
                Console.WriteLine("{0}: {1}",
                    string.IsNullOrEmpty(ib.Name) ? Path.GetFileName(ib.FileBase) : ib.Name,
                    mode);

                a.Open(ib);

                MetaProcessorReader pr = new MetaProcessorReader(a);
                List<MetaProcessor> list = pr.Get();
                foreach (MetaProcessor processor in list)
                {
                    Console.WriteLine("{0}. '{1}' ({2})", processor.Name, processor.Synonym, processor.Comments);
                }

                Console.WriteLine();

                string targetName = "Обработка1";
                bool contains = list.Find(
                    processor => processor.Name == targetName) != null;

                if (contains)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine("Found {0}", targetName);

                    object p = a.GlobalProperty("Обработки");
                    object my = p.Get(targetName);
                    object myInstance = my.Call("Создать");

                    try
                    {
                        object retVal = myInstance.Call("Привет", "");
                        retVal.ReleaseComObject();
                        Console.WriteLine("DONE");
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    my.ReleaseComObject();
                    p.ReleaseComObject();

                    Console.ForegroundColor = ConsoleColor.White;

                }
            }

        }


    }
}
